const CryptoJS = require('crypto-js'); //引用AES源码js

//解密方法
function Decrypt({
	encryptedData,
	session_key,
	iv
}) {
	// 推荐方式
	var dencryptedData = CryptoJS.enc.Base64.parse(encryptedData)
	var dkey = CryptoJS.enc.Base64.parse(session_key);
	var div = CryptoJS.enc.Base64.parse(iv);
	let o = CryptoJS.AES.decrypt({
		ciphertext: dencryptedData
	}, dkey, {
		iv: div
	})
	let jsonstr = CryptoJS.enc.Utf8.stringify(o)
	return JSON.parse(jsonstr)
}

//加密方法
function Encrypt({
	encryptedData,
	session_key,
	iv
}) {
	let srcs = CryptoJS.enc.Utf8.parse(encryptedData);
	let encrypted = CryptoJS.AES.encrypt(srcs, session_key, {
		iv: iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.ciphertext.toString().toUpperCase();
}

module.exports = {
	Decrypt,
	Encrypt
}
